<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Storage;

class MovieController extends Controller
{


    public function movieList()
    {
        $movies =  DB::table('movie')->where('type','1')->orderBy('movie_id','desc')->Paginate(5);

        return view('Admin/movieList')->with('movies',$movies);
    }

    public function movieAdd()
    {
        return view('Admin/movieAdd');

    }
    public function movieAdd2()
    {
        return view('Admin/movieAdd2');

    }


    public function doMovieAdd(Request $request)
    {
        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');
        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
            $movieData = $request->except(['_token','file','editorValue','dir','firstname','videoImg','carousel']);

            if(isset($data['file'])&&!empty($data['file'])){
                foreach ($data['file'] as $oneData){
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }

            }

            if ( isset( $data['carousel'] ) && !empty( $data['carousel'] ) )
            {
                $CarouselPath = array();
                foreach ($data['carousel'] as $item)
                {
                    $path = $this->doUpload($item);
                    array_push($CarouselPath, $path);
                }

                $CarouselString = '';

                foreach ($CarouselPath as $pathData){
                    if(in_array($pathData[0],$imageArray)){
                        $CarouselString .= $pathData['path'].'+';
                    }
                }

                $movieData['movie_carousel'] = $CarouselString;
            }

            $MyPath = $this->doUpload($data['firstname']);
            $MyDallPath = $this->doUpload($data['dollImg']);
            $MyOtherPath = $this->doUpload($data['videoImg']);

            $MStr = null;
            $ImageStr = null;
            $movieData['push_time'] = date("Y-m-d H:i:s");
            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }
                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }
            }

            $movieData['movie_image'] = $ImageStr;
            $movieData['vedio_id'] = $data['dir'];
            $movieData['movie_introduce'] = $data['editorValue'];
            $movieData['type'] = 1;
            $movieData['firstimg'] = $MyPath['path'];
            $movieData['dollImg'] = $MyDallPath['path'];
            $movieData['video_poster'] = $MyOtherPath['path'];


            $test = DB::table('movie')->insert($movieData);

            if($test){
                return redirect('admin/movieList');
                //return json_encode(['code'=>'1','msg'=>'success']);
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }
        }
    }

    public function doUpload($file){
        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        if ($file->isValid()) {
            // 获取文件相关信息
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $type = $file->getClientMimeType();     // image/jpeg
            // 上传文件
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
            if(true == $bool){
                $SQLFileName['path'] = 'uploads/'.$filename;
                array_push($SQLFileName,$ext);
                return $SQLFileName;
            }
        }

    }


    public function deleteMove(Request $request)
    {
        $id = $request->input('id');
        $result = DB::table('movie')
            ->where('movie_id','=',$id)
            ->delete();
        if(true == $result){
            return redirect('admin/movieList');
        }else{
            return redirect('admin/movieList');
        }

    }


    public function movieChange(Request $request)
    {
        $id = $request->input("id");
        $data = DB::table('movie')->where('movie_id',$id)->get();
//       dd($data);
        return view('Admin/movieChange')->with('movie',$data)->with('id',$id);

    }

    public function doChange(Request $request)
    {
        $id = $request->input("id");
        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
            $movieData = $request->except(['_token','file','editorValue','id', 'carousel']);

            if(!is_null($data['file'][0])){
                foreach ($data['file'] as $oneData){
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }
            }

            $MStr = null;
            $ImageStr = null;

            if ( isset( $data['carousel'] ) && !empty( $data['carousel'] ) )
            {
                $CarouselPath = array();
                foreach ($data['carousel'] as $item)
                {
                    $path = $this->doUpload($item);
                    array_push($CarouselPath, $path);
                }

                $CarouselString = '';

                foreach ($CarouselPath as $pathData){
                    if(in_array($pathData[0],$imageArray)){
                        $CarouselString .= $pathData['path'].'+';
                    }
                }

                $movieData['movie_carousel'] = $CarouselString;
            }

            $movieData['push_time'] = date("Y-m-d H:i:s");
            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }

                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }

            }
            if(isset($ImageStr)&&!empty($ImageStr)){
                $movieData['movie_image'] = $ImageStr;

            }

            if(isset($MStr)&&!empty($MStr)){
                $movieData['vedio_id'] = $MStr;
            }

            $myDollPath = $this->doUpload($data['dollImg']);
            if(isset($myDollPath)&&!empty($myDollPath)){
                $movieData['dollimg'] = $myDollPath['path'];
            }
            $movieData['movie_introduce'] = $data['editorValue'];

            $test = DB::table('movie')->where('movie_id', $id)
                ->update($movieData);

            if($test){
                return redirect('admin/movieList');
                //return json_encode(['code'=>'1','msg'=>'success']);
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }
        }
    }


    public function TVList()
    {
        $tvs =  DB::table('movie')->where('type','2')->orderBy('movie_id','desc')->Paginate(5);

        return view('Admin/TVList')->with('tvs',$tvs);

    }

    public function TVAdd()
    {
        return view('Admin/TVAdd');

    }

    public function doTVAdd(Request $request)
    {
        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
            if(isset($data['file'])&&!empty($data['file'])){
                foreach ($data['file'] as $oneData){
//                dd($oneData);
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }

            }


            $MyOtherPath = $this->doUpload($data['videoImg']);
            $MStr = null;
            $ImageStr = null;
            $movieData = $request->except(['_token','file','editorValue','videoImg','dijiji','youku']);

            $dijiji = [];
            $buShu = $data['dijiji'];
            $youKuLinks = $data['youku'];
            $k = 0;
            $n = count($buShu);
            if ($n > 0)
            {
                for ($j = 0; $j < count($buShu); ++$j)
                {
                    if ( $buShu[$j] != null && $buShu[$j] != '' && $youKuLinks[$j] != '' && $youKuLinks[$j] != null)
                    {
                        $dijiji[ $k ][ $buShu[$j] ] = $youKuLinks[ $j ];
                        $k++;
                    }
                }
                $movieData['dijiji'] = json_encode($dijiji);
            }
            //dd($dijiji);

            //dd($movieData);
//            $movieData['push_time'] = date("Y-m-d H:i:s");
            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }

                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }

            }
//            $dir = trim($request->input('dir'));
//            $MStr = strstr($dir, '.', TRUE);
////        var_dump($MStr);
//            mkdir(iconv('utf-8', 'gbk', $MStr));
//            $filmPath = "uploads/".$MStr."/".$dir;






            $movieData['movie_image'] = $ImageStr;
//            $movieData['video_id'] = $request->input('URL');
            $movieData['movie_introduce'] = $data['editorValue'];
            $movieData['type'] = 2;
            $movieData['video_poster'] = $MyOtherPath['path'];
//            dd($movieData);

            $test = DB::table('movie')->insert($movieData);

            if($test){
                return redirect('admin/TVList');
                //return json_encode(['code'=>'1','msg'=>'success']);
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }
        }

    }

    public function deleteTV(Request $request)
    {
        $id = $request->input('id');
        $result = DB::table('movie')
            ->where('movie_id','=',$id)
            ->delete();
        if(true == $result){
            return redirect('admin/TVList');
        }else{
            return redirect('admin/TVList');
        }
    }

    public function changeTV(Request $request)
    {
        $id = $request->input("id");
        $data = DB::table('movie')->where('movie_id',$id)->get();
        $dijiji = json_decode($data[0]->dijiji);
        //dd($dijiji);
//       dd($data);
        return view('Admin/TVChange')->with('tv',$data)->with('id',$id)->with('dijijis', $dijiji);

    }

    public function changeTVDo(Request $request)
    {
        $id = $request->input("id");
        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')) {
            $data = $request->all();
//            dd($data);

            if (!is_null($data['file'][0])) {

                foreach ($data['file'] as $oneData) {
//                dd($oneData);
                    $path = $this->doUpload($oneData);
                    array_push($allPath, $path);
                }

            }
            $MStr = null;
            $ImageStr = null;
            $movieData = $request->except(['_token', 'file', 'editorValue', 'id', 'dijiji', 'youku']);
//            $movieData['push_time'] = date("Y-m-d H:i:s");

            $dijiji = [];
            $buShu = $data['dijiji'];
            $youKuLinks = $data['youku'];
            $n = count($buShu);
            $k = 0;
            if ($n > 0)
            {
                for ($j = 0; $j < $n; ++$j)
                {
                    if ( $buShu[$j] != null && $buShu[$j] != '' && $youKuLinks[$j] != '' && $youKuLinks[$j] != null)
                    {
                        $dijiji[ $k ][ $buShu[$j] ] = $youKuLinks[ $j ];
                        $k++;
                    }
                }
                $movieData['dijiji'] = json_encode($dijiji);
            }

            foreach ($allPath as $pathData) {
                if (in_array($pathData[0], $imageArray)) {
                    $ImageStr .= $pathData['path'] . '+';
                }

                if (in_array($pathData[0], $movieArray)) {
                    $MStr .= $pathData['path'] . '+';
                }

            }
            if (isset($ImageStr) && !empty($ImageStr)) {
                $movieData['movie_image'] = $ImageStr;

            }

//            if(isset($MStr)&&!empty($MStr)){
//                $movieData['vedio_id'] = $MStr;
//            }

            $movieData['movie_introduce'] = $data['editorValue'];
//            dd($movieData);

            $test = DB::table('movie')->where('movie_id', $id)
                ->update($movieData);

            if ($test) {
                return redirect('admin/TVList');
                //return json_encode(['code' => '1', 'msg' => 'success']);
            } else {
                return json_encode(['code' => '0', 'msg' => 'faied']);
            }
        }
    }

    public function shortList()
    {
        $shorts =  DB::table('movie')->where('type','3')->orderBy('movie_id','desc')->Paginate(5);

        return view('Admin/shortList')->with('shorts',$shorts);

    }

    public function shortAdd()
    {
        return view('Admin/shortAdd');

    }

    public function doShortAdd(Request $request)
    {

        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
            if(isset($data['file'])&&!empty($data['file'])){
                foreach ($data['file'] as $oneData){
                    //dd($oneData);
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }

            }
            //dd($allPath);
            $MyOtherPath = $this->doUpload($data['videoImg']);
            $shortImg = $this->doUpload($data['shortImg']);
            $MStr = null;
            $ImageStr = null;
            $movieData = $request->except(['_token','file','editorValue','dir','videoImg','shortYOUKU']);
//            $movieData['push_time'] = date("Y-m-d H:i:s");
            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }

                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }

            }
            $dirs = $request->input('dir');
            $MyHost = $_SERVER['HTTP_ORIGIN'];
            $filmPath = '';
            //dd($dirs);
            if ( is_array($dirs) ) {
                if ( count($dirs) >= 1 ) {
                    foreach ($dirs as $dir) {
                        $MStr = strstr($dir, '.', TRUE);
                        $src = iconv('utf-8', 'gbk', $MStr);
                        if ( !file_exists($src) ) {
                            mkdir( $src );
                        }
                        $filmPath .= $MyHost."/".$MStr."/".$dir.'+';
                    }
                    $filmPath = substr($filmPath,0,strlen($filmPath)-1);
                }
                else {
                    $filmPath = '';
                }
            }
            else {
                $MStr = strstr($dirs, '.', TRUE);
//                mkdir(iconv('utf-8', 'gbk', $MStr));
//
//                $filmPath = $MyHost."/".$MStr."/".$dirs;
            }
            $movieData['shortImg'] = $shortImg['path'];
            $movieData['movie_image'] = $ImageStr;
            $movieData['vedio_id'] = $request->input('shortYOUKU');
            $movieData['movie_introduce'] = $data['editorValue'];
            $movieData['type'] = 3;
            $movieData['video_poster'] = $MyOtherPath['path'];
//            dd($movieData);
//            dd($movieData);

            $test = DB::table('movie')->insert($movieData);

            if($test){
                return redirect('admin/shortList');
                //return json_encode(['code'=>'1','msg'=>'success']);
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }
        } else {
            return json_encode(['code'=>'0','msg'=>'only accept post method']);
        }

    }

    public function deleteShort(Request $request)
    {
        $id = $request->input('id');
        $result = DB::table('movie')
            ->where('movie_id','=',$id)
            ->delete();
        if(true == $result){
            return redirect('admin/shortList');
        }else{
            return redirect('admin/shortList');
        }
    }

    public function changeShort(Request $request)
    {
        $id = $request->input("id");
        $data = DB::table('movie')->where('movie_id',$id)->get();
//       dd($data);
        return view('Admin/shortChange')->with('shortfilms',$data)->with('id',$id);

    }

    public function changeShortDo(Request $request)
    {
//        dd($request->all());
        $id = $request->input("id");
        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
//            dd($data);

            if(!is_null($data['file'][0])){

                foreach ($data['file'] as $oneData){
//                dd($oneData);
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }

            }
            $MStr = null;
            $ImageStr = null;
            $movieData = $request->except(['_token','file','editorValue','dir','id','shortYOUKU']);
//            $movieData['push_time'] = date("Y-m-d H:i:s");
            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }

                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }

            }
//            $dir = trim($request->input('dir'));
//            if( isset( $dir ) && !empty( $dir ) && !is_null( $dir )) {
//
//                $MStr = strstr($dir, '.', TRUE);
////        var_dump($MStr);
//                mkdir(iconv('utf-8', 'gbk', $MStr));
//                $filmPath = "uploads/".$MStr."/".$dir;
//                $movieData['vedio_id'] = $filmPath;
//            }


            $MyHost = $_SERVER['HTTP_ORIGIN'];
            $filmPath = '';
//            if ( $request->has('dir') ) {
//                $dirs = $request -> input('dir');
//                if ( is_array( $dirs ) ) {
//                    if ( count($dirs) >= 1 ) {
//                        foreach ($dirs as $dir) {
//                            $MStr = strstr($dir, '.', TRUE);
//                            $src = iconv('utf-8', 'gbk', $MStr);
//                            if ( !file_exists($src) ) {
//                                mkdir( $src );
//                            }
//                            $filmPath .= $MyHost."/".$MStr."/".$dir.'+';
//                        }
//                        $filmPath = substr($filmPath,0,strlen($filmPath)-1);
//                        $movieData['vedio_id'] = $filmPath;
//                    }
//                }
//            }

            $mine = $request->input('shortYOUKU');
            if (isset($mine)&&!empty($mine)){
                $movieData['vedio_id'] = $mine;
            }

            if (isset($ImageStr) && !empty($ImageStr)) {
                $movieData['movie_image'] = $ImageStr;
            }

//            if(isset($MStr)&&!empty($MStr)){
//                $movieData['vedio_id'] = $MStr;
//            }
            $movieData['movie_introduce'] = $data['editorValue'];

            $test = DB::table('movie')->where('movie_id', $id)
                ->update($movieData);

            if($test){
                return redirect('admin/shortList');
                //return json_encode(['code'=>'1','msg'=>'success']);
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }
        } else {
            return json_encode(['code'=>'0','msg'=>'only accept post method']);
        }
    }


    public function testCase(Request $request)
    {


        $b = scandir("timeromantic");
        dd($b);





    }








    //
    public function adList()
    {
        $shorts =  DB::table('movie')->where('type','4')->orderBy('movie_id','desc')->Paginate(5);

        return view('Admin/adList')->with('shorts',$shorts);

    }

    public function adAdd()
    {
        return view('Admin/adAdd');

    }

    public function doAdAdd(Request $request)
    {

        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
            if(isset($data['file'])&&!empty($data['file'])){
                foreach ($data['file'] as $oneData){
//                dd($oneData);
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }

            }
            $MyOtherPath = $this->doUpload($data['videoImg']);
            $MStr = null;
            $ImageStr = null;
            $movieData = $request->except(['_token','file','editorValue','dir','videoImg','adYOUKU']);
//            $movieData['push_time'] = date("Y-m-d H:i:s");
            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }

                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }

            }
            $dir = trim($request->input('dir'));
            $MStr = strstr($dir, '.', TRUE);
//            mkdir(iconv('utf-8', 'gbk', $MStr));
            $MyHost = $_SERVER['HTTP_ORIGIN'];
            $filmPath = $MyHost."/".$MStr."/".$dir;








            $movieData['movie_image'] = $ImageStr;
            $movieData['vedio_id'] = $request->input('adYOUKU');
            $movieData['movie_introduce'] = $data['editorValue'];
            $movieData['type'] = 4;
            $movieData['video_poster'] = $MyOtherPath['path'];
//            dd($movieData);

            $test = DB::table('movie')->insert($movieData);

            if($test){
                return redirect('admin/adList');
                //return json_encode(['code'=>'1','msg'=>'success']);
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }
        }

    }

    public function deleteAd(Request $request)
    {
        $id = $request->input('id');
        $result = DB::table('movie')
            ->where('movie_id','=',$id)
            ->delete();
        if(true == $result){
            return redirect('admin/adList');
        }else{
            return redirect('admin/adList');
        }
    }

    public function changeAd(Request $request)
    {
        $id = $request->input("id");
        $data = DB::table('movie')->where('movie_id',$id)->get();
//       dd($data);
        return view('Admin/adChange')->with('shortfilms',$data)->with('id',$id);

    }

    public function changeAdDo(Request $request)
    {
//        dd($request->all());
        $id = $request->input("id");
        $movieArray = array('mp4','mkv','3gp','avi','mov','wmv','flv','rmvb');
        $imageArray = array('jpg','bmp','gif','jpeg','psd','png');

        $allPath = array();
        if($request->isMethod('post')){
            $data = $request->all();
//            dd($data);

            if(!is_null($data['file'][0])){

                foreach ($data['file'] as $oneData){
//                dd($oneData);
                    $path = $this->doUpload($oneData);
                    array_push($allPath,$path);
                }

            }
            $MStr = null;
            $ImageStr = null;
            $movieData = $request->except(['_token','file','editorValue','dir','id','adYOUKU']);

//            $movieData['push_time'] = date("Y-m-d H:i:s");
            foreach ($allPath as $pathData){
                if(in_array($pathData[0],$imageArray)){
                    $ImageStr .= $pathData['path'].'+';
                }

                if(in_array($pathData[0],$movieArray)){
                    $MStr .= $pathData['path'].'+';
                }

            }
            $dir = trim($request->input('dir'));
//            if(isset($dir)&&!empty($dir)&&!is_null($dir)){
//                $MStr = strstr($dir, '.', TRUE);
////        var_dump($MStr);
//                mkdir(iconv('utf-8', 'gbk', $MStr));
//                $filmPath = "uploads/".$MStr."/".$dir;
//                $movieData['vedio_id'] = $filmPath;
//            }
            $mine = $request->input('adYOUKU');

            if (isset($mine)&&!empty($mine)){
                $movieData['vedio_id'] = $mine;
            }




            if (isset($ImageStr) && !empty($ImageStr)) {
                $movieData['movie_image'] = $ImageStr;

            }

            if(isset($MStr)&&!empty($MStr)){
                $movieData['vedio_id'] = $MStr;
            }

            $movieData['movie_introduce'] = $data['editorValue'];
//            dd($movieData);

            $test = DB::table('movie')->where('movie_id', $id)
                ->update($movieData);

            if($test){
                return redirect('admin/adList');
                //return json_encode(['code'=>'1','msg'=>'success']);
            }else{
                return json_encode(['code'=>'0','msg'=>'faied']);
            }



        }

    }
}
